<template>
	<view>
		 <view class="goods-list">
		      <view v-for="(goods, i) in goodsList" :key="i" @click="gotoGoodsDetail(goods)">  <my-goods :goods="goods"></my-goods>
		        </view>
		    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				    queryObj: {
				      // 查询关键词
				      query: '',
				      // 商品分类Id
				      cid: '',
				      // 页码值
				      pagenum: 1,
				      // 每页显示多少条数据
				      pagesize: 10
				    },
            // 商品列表数据
            goodsList: [],
            // 商品数目
            total: 0,
            // 上拉触底事件节流阀
            flag: false
			};
		},
    onLoad(options) {
      this.queryObj.query = options.query || ''
       this.queryObj.cid = options.cid || ''
       this.getGoodsList()
    },
    onReachBottom () {
      if(this.flag) return
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total ) return uni.$showMsg("数据加载完毕")
      this.queryObj.pagenum ++
      this.getGoodsList()
    },
    onPullDownRefresh () {
      this.queryObj.pagenum = 1
      this.total = 0
      this.goodsList = []
      this.flag = false
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    methods: {
      // 获取商品列表数据
      async getGoodsList (cb) {
        this.flag = true
        const { data: res } = await uni.$http.get('/goods/search',this.queryObj)
        this.flag = false
        cb && cb()
        console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg('获取商品列表失败')
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },
      gotoGoodsDetail (goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
    }
	}
</script>

<style lang="scss">

</style>
